#include <iostream>
#include <stdint.h>

#define	MY_GET_BYTE0(x)	((x >>  0) & 0x000000ff)	/* 获取第0个字节 */
#define	MY_GET_BYTE1(x)	((x >>  8) & 0x000000ff)	/* 获取第1个字节 */
#define	MY_GET_BYTE2(x)	((x >> 16) & 0x000000ff)	/* 获取第2个字节 */
#define	MY_GET_BYTE3(x)	((x >> 24) & 0x000000ff)	/* 获取第3个字节 */
#define	MY_CLEAR_BYTE0(x)	(x &= 0xffffff00)	/* 清零第0个字节 */
#define	MY_CLEAR_BYTE1(x)	(x &= 0xffff00ff)	/* 清零第1个字节 */
#define	MY_CLEAR_BYTE2(x)	(x &= 0xff00ffff)	/* 清零第2个字节 */
#define	MY_CLEAR_BYTE3(x)	(x &= 0x00ffffff)	/* 清零第3个字节 */
#define	MY_SET_BYTE0(x)  (x |= 0x000000ff)	/* 第0个字节置1 */
#define	MY_SET_BYTE1(x)  (x |= 0x0000ff00)	/* 第1个字节置1 */
#define	MY_SET_BYTE2(x)  (x |= 0x00ff0000)	/* 第2个字节置1 */
#define	MY_SET_BYTE3(x)  (x |= 0xff000000)	/* 第3个字节置1 */
#define	MY_GET_BIT(x, bit)	((x & (1 << bit)) >> bit)	/* 获取第bit位 */
#define	MY_CLEAR_BIT(x, bit)	(x &= ~(1 << bit))	/* 清零第bit位 */
#define	MY_SET_BIT(x, bit)	(x |= (1 << bit))	/* 置位第bit位 */

int main()
{
    unsigned int a=0x44332211;
    unsigned int a_copy=0x44332211;

    printf("0x%x的第0个字节为：0x%x\n",a, MY_GET_BYTE0(a));
    printf("0x%x的第1个字节为：0x%x\n",a, MY_GET_BYTE1(a));
    printf("0x%x的第2个字节为：0x%x\n",a, MY_GET_BYTE2(a));
    printf("0x%x的第3个字节为：0x%x\n",a, MY_GET_BYTE3(a));

    a=0x44332211;
    printf("0x%x的第0个字节清零后变为：0x%08x\n",a_copy, MY_CLEAR_BYTE0(a));
    a=0x44332211;
    printf("0x%x的第1个字节清零后变为：0x%08x\n",a_copy, MY_CLEAR_BYTE1(a));
    a=0x44332211;
    printf("0x%x的第2个字节清零后变为：0x%08x\n",a_copy, MY_CLEAR_BYTE2(a));
    a=0x44332211;
    printf("0x%x的第3个字节清零后变为：0x%08x\n",a_copy, MY_CLEAR_BYTE3(a));

    a=0x44332211;
    printf("0x%x的第0个字节置1后变为：0x%08x\n",a_copy, MY_SET_BYTE0(a));
    a=0x44332211;
    printf("0x%x的第1个字节置1后变为：0x%08x\n",a_copy, MY_SET_BYTE1(a));
    a=0x44332211;
    printf("0x%x的第2个字节置1后变为：0x%08x\n",a_copy, MY_SET_BYTE2(a));
    a=0x44332211;
    printf("0x%x的第3个字节置1后变为：0x%08x\n",a_copy, MY_SET_BYTE3(a));

    unsigned int b=0x51;
 
    printf("0x%x的对应的二进制是：%x %x %x %x %x %x %x %x \n",b,MY_GET_BIT(b,7),MY_GET_BIT(b,6),MY_GET_BIT(b,5),MY_GET_BIT(b,4),MY_GET_BIT(b,3),MY_GET_BIT(b,2),MY_GET_BIT(b,1),MY_GET_BIT(b,0));
    printf("0x%x的第0个bit的值是：%x\n",b,MY_GET_BIT(b,0));
    printf("0x%x的第1个bit的值是：%x\n",b,MY_GET_BIT(b,1));
    printf("0x%x的第2个bit的值是：%x\n",b,MY_GET_BIT(b,2));
    printf("0x%x的第3个bit的值是：%x\n",b,MY_GET_BIT(b,3));
    printf("0x%x的第3个bit的值是：%x\n",b,MY_GET_BIT(b,4));
    printf("0x%x的第3个bit的值是：%x\n",b,MY_GET_BIT(b,5));
    printf("0x%x的第3个bit的值是：%x\n",b,MY_GET_BIT(b,6));
    printf("0x%x的第3个bit的值是：%x\n",b,MY_GET_BIT(b,7));

    unsigned int a0=0x81;   
    unsigned int a0_copy=a0;   
    unsigned int a1=0x82;
    unsigned int a1_copy=a0;   
    unsigned int a2=0x84;
    unsigned int a2_copy=a0;   
    unsigned int a3=0x88;
    unsigned int a3_copy=a0;   

    printf("0x%x的第0个bit清零后的值是：%#.2x\n",a0_copy,MY_CLEAR_BIT(a0, 0));
    printf("0x%x的第1个bit清零后的值是：%#.2x\n",a1_copy,MY_CLEAR_BIT(a1, 1));
    printf("0x%x的第2个bit清零后的值是：%#.2x\n",a2_copy,MY_CLEAR_BIT(a2, 2));
    printf("0x%x的第3个bit清零后的值是：%#.2x\n",a3_copy,MY_CLEAR_BIT(a3, 3));

    unsigned int b0=0x10;
    unsigned int b1=0x10;
    unsigned int b2=0x10;
    unsigned int b3=0x10;

    printf("0x10的第0个bit置1后的值是：%#.2x\n",MY_SET_BIT(b0, 0));
    printf("0x10的第1个bit置1后的值是：%#.2x\n",MY_SET_BIT(b1, 1));
    printf("0x10的第2个bit置1后的值是：%#.2x\n",MY_SET_BIT(b2, 2));
    printf("0x10的第3个bit置1后的值是：%#.2x\n",MY_SET_BIT(b3, 3));

    return 0;
}